package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ShopDivisionReceiverGroup;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.ShopDivisionReceiverGroupMapper;
import com.yami.shop.service.ShopDivisionReceiverGroupService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class ShopDivisionReceiverGroupServiceImpl extends ServiceImpl<ShopDivisionReceiverGroupMapper, ShopDivisionReceiverGroup> implements ShopDivisionReceiverGroupService {

    private final ShopDivisionReceiverGroupMapper shopDivisionReceiverGroupMapper;

    @Override
    public ShopDivisionReceiverGroup findByIdAndShopId(Long groupId, Long shopId) {
        return getOne(new LambdaQueryWrapper<ShopDivisionReceiverGroup>().eq(ShopDivisionReceiverGroup::getReceiverGroupId, groupId).eq(ShopDivisionReceiverGroup::getShopId, shopId));
    }

    @Override
    public IPage<ShopDivisionReceiverGroup> pageByParam(PageParam<ShopDivisionReceiverGroup> page, ShopDivisionReceiverGroup shopDivisionReceiverGroup) {
        LambdaQueryWrapper<ShopDivisionReceiverGroup> condition = new LambdaQueryWrapper<>();
        condition.eq(ShopDivisionReceiverGroup::getShopId, shopDivisionReceiverGroup.getShopId());
        if(StringUtils.isNotEmpty(shopDivisionReceiverGroup.getReceiverGroupName())){
            condition.like(ShopDivisionReceiverGroup::getReceiverGroupName, shopDivisionReceiverGroup.getReceiverGroupName());
        }
        if(shopDivisionReceiverGroup.getReceiverGroupId() != null){
            condition.eq(ShopDivisionReceiverGroup::getReceiverGroupId, shopDivisionReceiverGroup.getReceiverGroupId());
        }
        condition.orderByDesc(ShopDivisionReceiverGroup::getCreateTime); //时间倒序
        return shopDivisionReceiverGroupMapper.selectPage(page,condition);
    }
}
